import { defineStore } from "pinia";
import { ref } from "vue";
import { userInfoApi } from "@/api/user.js";
import router from "../router";
//用户模块
export const useUserStore = defineStore(
  "big-user",
  () => {
    const token = ref("");
    const setToken = (newToken) => {
      token.value = newToken;
    };
    const removeToken = () => {
      token.value = "";
    };
    const user = ref({});
    const alreadyAsyncRoutes = ref(false);
    const updataAsyncRoutes = () => {
      alreadyAsyncRoutes.value = true;
    };
    const getUser = async () => {
      const res = await userInfoApi();
      user.value = res.data;
      user.value.MenuList = [
        {
          id: 22,
          name: "task:manage",
          title: "任务管理",
          icon: "Promotion",
          path: "task",
          component: "",
          children: [
            {
              id: 24,
              name: "center",
              title: "任务中心",
              icon: "Promotion",
              path: "/task/center",
              children: [],
            },
            {
              id: 25,
              name: "task:audit:list",
              title: "任务审核",
              icon: "Tickets",
              path: "/task/audit",
              component: "task/audit",
              children: [],
            },
            {
              id: 26,
              name: "task:execute:list",
              title: "任务执行",
              icon: "Finished",
              path: "/task/execute",
              component: "task/execute",
              children: [],
            },
          ],
        },
        {
          component: "",
          icon: "PieChart",
          id: 31,
          name: "comprehensive:manage",
          path: "apply",
          title: "综合应用",
          children: [
            {
              children: [],
              component: null,
              icon: "Message",
              id: 46,
              name: "news:list",
              path: "/comprehensive/news",
              title: "新闻动态",
            },
          ],
        },
        {
          children: [],
          icon: "Promotion",
          id: 1,
          name: "sys:manage",
          path: "/employees",
          title: "组织架构",
        },
        {
          component: "",
          icon: "Tickets",
          id: 27,
          name: "message:manage",
          title: "信息管理",
          path: "manage",
          children: [
            {
              children: [],
              component: "message/population",
              icon: "Connection",
              id: 29,
              name: "message:list",
              path: "/message/population",
              title: "人口信息",
            },
            {
              children: [],
              component: "message/house",
              icon: "Odometer",
              id: 30,
              name: "message:house:list",
              path: "/message/house",
              title: "房产信息",
            },
          ],
        },
        {
          component: null,
          icon: "Crop",
          id: 33,
          name: "declare:manage",
          path: "declare",
          title: "综合管理",
          children: [
            {
              children: [],
              component: "",
              icon: "Location",
              id: 34,
              name: "declare:list",
              path: "/declare/manage",
              title: "模板管理",
            },
            {
              component: "",
              children: [],
              icon: "IceTea",
              id: 39,
              name: "declare:regualtion",
              path: "/patrol/regualtion",
              title: "条例管理",
            },
            {
              children: [],
              component: "",
              icon: "Soccer",
              id: 35,
              name: "declare:type",
              path: "/declare/type",
              title: "类型管理",
            },
            {
              children: [],
              component: "",
              icon: "Suitcase",
              id: 36,
              name: "declare:data",
              path: "/declare/data",
              title: "基础数据",
            },
            {
              children: [],
              component: "",
              icon: "ShoppingTrolley",
              id: 44,
              name: "declare:rule",
              path: "/declare/rule",
              title: "治理数据",
            },
          ],
        },
      ];
      addAsyncRoute();
    };
    const setUser = (obj) => {
      user.value = obj;
    };

    // 侧边栏是否展开
    const isExpand = ref(true);
    // 展开
    const toggleExpand = () => {
      isExpand.value = !isExpand.value;
    };
    const isScreen = ref(false);
    const toggleScreen = () => {
      isScreen.value = !isScreen.value;
    };
    const closeScreen = () => {
      isScreen.value = false;
    };

    const addAsyncRoute = () => {
      // 获取 前端定义的所有需要权限的路由
      const allRoutes = [];
      const files = import.meta.glob("@/router/modules/**/*.js", {
        eager: true,
      });
      console.log(files, "files");
      for (const key in files) {
        if (Object.hasOwnProperty.call(files, key)) {
          if (files[key].default) {
            allRoutes.push(...files[key].default);
          }
        }
      }
      const backRoutes = user.value.MenuList;
      allRoutes.forEach((x) => {
        router.addRoute("layout", x);
      });
      // console.log(backRoutes, "后端返回的");

      updataAsyncRoutes();
      console.log(allRoutes, "导入前端定义的所有动态路由");
      routerHandler(backRoutes, allRoutes);
    };
    const routerHandler = (userRoutes, allRoutes) => {
      const realRoutrs = [];
      allRoutes.forEach((v, i) => {
        userRoutes.forEach((item, index) => {
          if (item.title === v.title) {
            if (item.children && item.children.length > 0) {
              v.children = routerHandler(item.children, v.children);
            }
            realRoutrs.push(v);
          }
        });
      });
      user.value.MenuList = realRoutrs;
      console.log(realRoutrs, "最终的路由");
      return realRoutrs;
    };
    const tags = ref([]);
    const changeRags = (obj) => {
      const hasTag = tags.value.find((item) => item.name === obj.name);
      if (!hasTag) {
        tags.value.push({ name: obj.name, url: obj.url });
      }
    };
    const removeTag = (obj) => {
      tags.value = tags.value.filter((item) => item.name !== obj);
    };
    const setTags = (obj) => {
      tags.value = obj;
    };
    return {
      tags,
      changeRags,
      setTags,
      removeTag,
      token,
      setToken,
      removeToken,
      user,
      getUser,
      setUser,
      isExpand,
      toggleExpand,
      isScreen,
      toggleScreen,
      closeScreen,
      addAsyncRoute,
      alreadyAsyncRoutes,
      updataAsyncRoutes,
    };
  },
  { persist: true }
);
